﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class creatotZhu : MonoBehaviour {
    Vector3[] normals;
    Vector3[] verts;
    Vector2[] uvs;
    float angle;
    public GameObject testMesh;

    int edg_x = 10;
    int edg_y = 2;
    float rad = 2;
    static GameObject test;
    private void Start () {
        // // Mesh newMesh = CreateMesh (10, 1, 1, 30);
        // Mesh newMesh = creatorShengZi (new Vector3 (18, 32.4f, 0), new Vector3 (-6, 30, 0));
        // this.testMesh.GetComponent<MeshFilter> ().mesh = newMesh;
        // MeshCollider newCollider = this.testMesh.AddComponent<MeshCollider> ();
        // newCollider.convex = true;
        test = testMesh;
        List<Vector3> testMeshsss = new List<Vector3> ();
        testMeshsss.Add (new Vector3 (10, 0, 0));
        testMeshsss.Add (new Vector3 (-10, 0, 0));
        testMeshsss.Add (new Vector3 (10, 50, 0));
        testMeshsss.Add (new Vector3 (-10, 50, 0));
        testMeshsss.Add (new Vector3 (30, 10, 0));
        testMeshsss.Add (new Vector3 (25, -10, 0));
        // test2dRop (testMeshsss);
    }
    public Mesh CreateMesh (int edg_x, int edg_y, float rad, float len) {
        edg_x = Mathf.Max (2, edg_x); //保证最低2个边
        edg_y = Mathf.Max (2, edg_y);
        int _deglen = edg_x * edg_y + edg_y;
        normals = new Vector3[_deglen];
        verts = new Vector3[_deglen];
        uvs = new Vector2[_deglen];
        int[] trians = new int[edg_x * (edg_y - 1) * 6];
        float reg = 6.28318f / edg_x;
        float _len = len / (edg_y - 1);
        Vector2 uvStep = new Vector2 (1f / edg_x, 1f / (edg_y - 1));
        for (int y = 0; y < edg_y; y++)
            for (int x = 0; x < edg_x + 1; x++) //多一个边来保存UV值
        {
            int i = x + y * (edg_x + 1);
            verts[i] = new Vector3 (Mathf.Sin ((reg * (x % edg_x) + angle) % 6.28318f) * rad, Mathf.Cos ((reg * (x % edg_x) + angle) % 6.28318f) * rad, y * _len); //计算顶点坐标
            normals[i] = -new Vector3 (verts[i].x, verts[i].y, 0); //计算法线方向
            int id = x % (edg_x + 1) * 6 + y * edg_x * 6;
            if (x < edg_x + 1 && y < edg_y - 1 && (id + 5) < trians.Length) //计算顶点数组
            {
                trians[id] = i;
                trians[id + 1] = trians[id + 4] = i + edg_x + 1;
                trians[id + 2] = trians[id + 3] = i + 1;
                trians[id + 5] = i + edg_x + 2;
            }
            if (edg_x != 2) //计算UV，考虑到2个边的情况
                uvs[i] = new Vector2 (x == edg_x ? 1f : uvStep.x * x, y == edg_y - 1 ? 1f : uvStep.y * y);
            else
                uvs[i] = new Vector2 (x % edg_x, y == edg_y - 1 ? 1f : uvStep.y * y);
        }
        Mesh mesh = new Mesh ();
        mesh.vertices = verts;
        mesh.triangles = trians;
        mesh.uv = uvs;
        mesh.normals = normals;
        return mesh;
    }
    //绳子
    public Mesh creatorShengZi (Vector3 startP, Vector3 endP) {
        float len = Vector3.Distance (startP, endP);
        edg_x = Mathf.Max (2, edg_x); //保证最低2个边
        edg_y = Mathf.Max (2, edg_y);
        int _deglen = edg_x * edg_y + edg_y;
        normals = new Vector3[_deglen];
        verts = new Vector3[_deglen];
        uvs = new Vector2[_deglen];
        int[] trians = new int[edg_x * (edg_y - 1) * 6];
        float reg = 6.28318f / edg_x;
        float _len = len / (edg_y - 1);
        Vector2 uvStep = new Vector2 (1f / edg_x, 1f / (edg_y - 1));
        for (int y = 0; y < edg_y; y++) {
            for (int x = 0; x < edg_x + 1; x++) { //多一个边来保存UV值
                int i = x + y * (edg_x + 1);
                verts[i] = startP + new Vector3 (y * _len, Mathf.Cos ((reg * (x % edg_x) + angle) % 6.28318f) * rad, Mathf.Sin ((reg * (x % edg_x) + angle) % 6.28318f) * rad); //计算顶点坐标
                normals[i] = -new Vector3 (verts[i].x, verts[i].y, 0); //计算法线方向
                int id = x % (edg_x + 1) * 6 + y * edg_x * 6;
                if (x < edg_x + 1 && y < edg_y - 1 && (id + 5) < trians.Length) { //计算顶点数组
                    trians[id] = i;
                    trians[id + 1] = trians[id + 4] = i + edg_x + 1;
                    trians[id + 2] = trians[id + 3] = i + 1;
                    trians[id + 5] = i + edg_x + 2;
                }
                if (edg_x != 2) { //计算UV，考虑到2个边的情况
                    uvs[i] = new Vector2 (x == edg_x ? 1f : uvStep.x * x, y == edg_y - 1 ? 1f : uvStep.y * y);
                } else {
                    uvs[i] = new Vector2 (x % edg_x, y == edg_y - 1 ? 1f : uvStep.y * y);
                }
            }
        }

        Mesh mesh = new Mesh ();
        mesh.vertices = verts;
        mesh.triangles = trians;
        mesh.uv = uvs;
        mesh.normals = normals;
        return mesh;
    }
    //测试2d绳子
    public static Mesh test2dRop (List<Vector3> arrVertices) {
        int width = 2;
        int dianCount = arrVertices.Count / 2;
        List<Vector3> newVertices = new List<Vector3> ();
        int bianCount = 20;
        float angle = 360 / bianCount;
        int[] trians = new int[bianCount * (dianCount - 1) * 6];
        Vector2[] uvs = new Vector2[bianCount * dianCount];
        Vector3[] normals = new Vector3[bianCount * dianCount];
        for (int i = 0; i < dianCount; i++) {
            Vector3 neiP = arrVertices[i * 2];
            Vector3 waiP = arrVertices[i * 2 + 1];
            neiP.z = 0;
            waiP.z = 0;
            Vector3 centerP = new Vector3 ((neiP.x + waiP.x) / 2, (neiP.y + waiP.y) / 2, (neiP.z + waiP.z) / 2);
            Vector3 oriVec = (waiP - centerP) * width;
            Vector3 axis = Quaternion.AngleAxis (90, Vector3.forward) * oriVec;
            for (int j = 0; j < bianCount; j++) {
                Vector3 newVec = Quaternion.AngleAxis (angle * j, axis) * oriVec;
                Vector3 newp = newVec + centerP;
                newVertices.Add (newp);
                int id = i * bianCount * 6 + j * 6;
                int b = i * bianCount + j;
                int extra2 = j == bianCount - 2 ? bianCount : 0;
                int extra = j == bianCount - 1 ? bianCount : 0;
                if (i < dianCount - 1) {
                    trians[id] = b;
                    trians[id + 1] = b + bianCount + 1 - extra;
                    trians[id + 2] = b + 1 - extra;
                    trians[id + 3] = b + 1 - extra;
                    trians[id + 4] = b + bianCount + 1 - extra;
                    trians[id + 5] = b + bianCount + 2 - extra - extra2;
                }
                uvs[b] = new Vector2 (0, 1);
                normals[b] = Vector3.Normalize(Quaternion.AngleAxis(angle * (j + 0.5f), axis) * oriVec);
                // normals[b] = Vector3.zero;
            }
        }
        // for (int i = 0; i < newVertices.Count / bianCount; i++) {
        //     for (int j = 0; j < bianCount - 1; j++) {
        //         Debug.DrawLine (newVertices[i * bianCount + j], newVertices[i * bianCount + j + 1], Color.red);
        //     }
        // }

        Mesh mesh = new Mesh ();
        mesh.name = "sdfs";
        mesh.vertices = newVertices.ToArray ();
        mesh.triangles = trians;
        mesh.uv = uvs;
        mesh.normals = normals;

        test.GetComponent<MeshFilter> ().mesh = mesh;

        // test.GetComponent<MeshCollider> ().convex = true;
        // MeshCollider newCollider = test.AddComponent<MeshCollider> ();
        // newCollider.convex = true;
        // SaveAsset ();
        return mesh;
    }
    void SaveAsset () {
        Mesh mesh = test.GetComponent<MeshFilter> ().mesh;
        AssetDatabase.CreateAsset (mesh, "Assets/" + "myMesh" + ".FBX");
    }
}